#include<stdio.h>

int * append(int *a,int n){
    int i;
    int array[n+1];
    for(i=0;i<n;i++){
        array[i]=*(a+i);}
    printf("Enter the number to be added : ");
    scanf("%d",array+n);
    int *a2=&array[0];
    return(a2);
}

int * insert(int *a,int n){
    int i,x;
    int array[n+1];
    printf("Enter the position it has to be added to : ");
    scanf("%d",&x);
    if(x<=n && x>0){
        x=x-1;
        printf("Enter the number to be added : ");
        scanf("%d",&array[x]);
        if(x<n){
            for(i=0;i<x;i++){
                array[i]=*(a+i);}
            for(i=x+1;i<n+1;i++){
                array[i]=*(a+i-1);}
            int *a2=&array[0];
            return(a2);}}
    else if(x<=0){
        for(i=1;i<n+1;i++){
            array[i]=*(a+i-1);}
        printf("Enter the number to be added : ");
        scanf("%d",&array[0]);
        int *a2=&array[0];
        return(a2);}
    else{
    for(i=0;i<n;i++){
        array[i]=*(a+i);}
    printf("Enter the number to be added : ");
    scanf("%d",&array[n]);
    int *a2=&array[0];
    return(a2);}
}

int *sort(int *a,int n){
    int key,i,j;
    for(i=0;i<n-1;i++){
        for(j=i+1;j<n;j++){
            if(*(a+i)>*(a+j)){
                key=*(a+i);
                *(a+i)=*(a+j);
                *(a+j)=key;}}}
    return(a);}

int *reverse(int *a,int n){
    int i,key,j;
    for(i=0;i<n/2;i++){
        j=n-1-i;
        key=*(a+i);
        *(a+i)=*(a+j);
        *(a+j)=key;}
    printf("\n");
    return(a);}

int count(int *a,int n){
    int num,sum,i;
    sum=0;
    printf("Enter the number: ");
    scanf("%d",&num);
    int flag=0;
    for(i=0;i<n;i++){
        if (num==*(a + i)){
        sum=sum+1;}}
    printf("The number of times the number %d occurs is %d \n",num,sum);
    return(num);}


void main(){
    int n,i,*a;
    printf("Enter the number length of list:  ");
    scanf("%d",&n);
    int array[n];
    printf("Enter the list:  ");
    for(i=0;i<n;i++){
        scanf("%d",&array[i]);
        a=&array[0];
        }
    main2(&array[0],n);}

void main2(int *array,int n){
    int *a=array;
    int *b;
    int i;
    char ch;
    printf("Enter i=insert , a=append , s=sort , r=reverse , n=index , c=count , d=delete , p=pop , e=extend , t=terminate : ");
    scanf("%c",&ch);
    scanf("%c",&ch);
    while((ch!='e'){
        if(ch=='a'){
            for(i=0;i<n;i++){
                printf("%d ",*(a+i));}
            printf("\n");
            b=append(a,n);
            for(i=0;i<n+1;i++){
                *(a+i)=*(b+i);}
            for(i=0;i<n+1;i++){
                printf("%d ",*(a+i));}
            printf("\n");
            n=n+1;}

        else if(ch=='i'){
            for(i=0;i<n;i++){
            printf("%d ",*(a+i));}
            printf("\n");
            b=insert(a,n);
            for(i=0;i<n+1;i++){
                *(a+i)=*(b+i);}
            for(i=0;i<n+1;i++){
                printf("%d ",*(a+i));}
            printf("\n");
            n=n+1;}

        else if(ch=='s'){
            b=sort(a,n);
            for(i=0;i<n;i++){
                *(a+i)=*(b+i);}
            for(i=0;i<n;i++){
                printf("%d ",*(a+i));}
            printf("\n");
            }


        else if(ch=='r'){
            b=sort(a,n);
            for(i=0;i<n;i++){
                *(a+i)=*(b+i);}
            for(i=0;i<n;i++){
                printf("%d ",*(a+i));}
            printf("\n");
            }

        else if(ch=='c'){
            int num=count(a,n);
            }
        printf("Enter i=insert , a=append , s=sort , r=reverse , n=index , c=count , d=delete , p=pop , e=extend , t=terminate : ");
        scanf("%c",&ch);
        scanf("%c",&ch);
    }
    printf("Final list : ");
    for(i=0;i<n;i++){
        printf("%d ",*(a+i));}
    printf("\n                THANK YOU\n");
}